package net.zigin.nhi.could.waste.util;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 主要分三类 百度 达观 通用
 */
public class HttpClientUtil {

    public static String post(String url, Object param) {
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        if (Objects.nonNull(param)) {
            if (param instanceof String) {
                httpPost.setEntity(new StringEntity(param.toString(), StandardCharsets.UTF_8));
            } else {
                httpPost.setEntity(new StringEntity(JSON.toJSONString(param), StandardCharsets.UTF_8));
            }
        }

        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setConfig(RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build());

        HttpResponse execute = null;
        try {
            execute = httpClient.execute(httpPost);
            int statusCode = execute.getStatusLine().getStatusCode();

            if (200 == statusCode) {
                return EntityUtils.toString(execute.getEntity());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String smsCode(String url, String mobile) {
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        try {
            List<NameValuePair> parameters = new ArrayList<>();
            parameters.add(new BasicNameValuePair("mobile", mobile));
            HttpEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
            httpPost.setEntity(entity);
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            httpPost.setConfig(RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build());

            HttpResponse execute = null;

            execute = httpClient.execute(httpPost);
            HttpEntity httpEntity = execute.getEntity();
            if (httpEntity != null) {
                InputStream instreams = httpEntity.getContent();
                String str = convertStreamToString(instreams);
                httpPost.abort();
                return str;
            } else {
                return "";
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String convertStreamToString(InputStream is) {
        StringBuilder sb1 = new StringBuilder();
        byte[] bytes = new byte[4096];
        int size = 0;

        try {
            while ((size = is.read(bytes)) > 0) {
                String str = new String(bytes, 0, size, "UTF-8");
                sb1.append(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb1.toString();
    }

    public static String get(String url) {
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build());

        HttpResponse execute = null;
        try {
            execute = httpClient.execute(httpGet);
            int statusCode = execute.getStatusLine().getStatusCode();
            if (200 == statusCode) {
                return EntityUtils.toString(execute.getEntity());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

}
